<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>EASY后台-代码生成</title>
    <link rel="icon" th:href="@{/static/images/favicon.ico}">
    <link rel="stylesheet" th:href="@{/static/css/common.css}">
    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}">
</head>
<body>
<!-- 页面内容 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 搜索条件 -->
            <form class="layui-form">
                <div class="layui-form-item">
                    <label class="layui-form-label">表名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="tableName" placeholder="请输入" class="layui-input">
                    </div>
                    <label class="layui-form-label">表注释</label>
                    <div class="layui-input-inline">
                        <input type="text" name="tableComment" placeholder="请输入" class="layui-input">
                    </div>
                    <button class="layui-btn layui-btn-md" lay-submit lay-filter="tableSearch">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="layui-btn layui-btn-primary layui-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </form>
            <!-- 表格 -->
            <table class="layui-hide" id="dataTable" lay-filter="dataTable"></table>
        </div>
    </div>
</div>
</body>

<!-- 表格工具 -->
<script type="text/html" id="tableToolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">
            <i class="layui-icon layui-icon-addition"></i>新建
        </button>
        <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="import">
            <i class="layui-icon layui-icon-download-circle"></i>导入
        </button>
        <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="batchRemove">
            <i class="layui-icon layui-icon-delete"></i>删除
        </button>
    </div>
</script>

<!-- 表格行工具 -->
<script type="text/html" id="tableRowTool">
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">配置</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="remove">删除</a>
    <a class="layui-btn layui-btn-checked layui-btn-xs" lay-event="genLocal">代码生成</a>
    <a class="layui-btn layui-btn-checked layui-btn-xs" lay-event="genDownload">代码下载</a>
</script>

<script th:src="@{/static/js/jquery.js}"></script>
<script th:src="@{/static/layui/layui.js}"></script>
<script>
    layui.use(['element', 'layer', 'table', 'laydate', 'form'], function () {
        var element = layui.element;
        var layer = layui.layer;
        var table = layui.table;
        var laydate = layui.laydate;
        var form = layui.form;

        // table渲染
        var tableObj = table.render({
            elem: '#dataTable',
            url: '/genTable/page',
            method: 'GET',
            request: {
                pageName: 'pageNum',
                limitName: 'pageSize'
            },
            where: { // 添加额外条件

            },
            toolbar: '#tableToolbar',
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [
                [
                    {type: 'checkbox', fixed: 'left'},
                    {type: 'numbers', title: '序号', align: 'center'},
                    {
                        field: 'tableType', title: '创建方式', width: 100, align: 'center',
                        templet: function (row) {
                            return row.tableType === '1' ? '新增' : '导入';
                        }
                    },
                    {field: 'tableName', title: '表名', width: 100, align: 'center'},
                    {field: 'tableComment', title: '表注释', width: 100, align: 'center'},
                    {
                        field: 'extendBaseEntity', title: '继承基类', width: 100, align: 'center',
                        templet: function (row) {
                            return row.extendBaseEntity === true ? '是' : '否';
                        }
                    },
                    {field: 'projectName', title: '项目名', width: 100, align: 'center'},
                    {field: 'genAuthor', title: '作者', width: 100, align: 'center'},
                    {field: 'genVersion', title: '版本号', width: 100, align: 'center'},
                    {field: 'genPackage', title: '包路径', width: 100, align: 'center'},
                    {field: 'genClass', title: '类名', width: 100, align: 'center'},
                    {field: 'genFunction', title: '功能描述', width: 100, align: 'center'},
                    {
                        field: 'genOperateLog', title: '日志', width: 100, align: 'center',
                        templet: function (row) {
                            return row.genOperateLog === true ? '是' : '否';
                        }
                    },
                    {
                        field: 'genAdd', title: '增', width: 100, align: 'center',
                        templet: function (row) {
                            return row.genAdd === true ? '是' : '否';
                        }
                    },
                    {
                        field: 'genRemove', title: '删', width: 100, align: 'center',
                        templet: function (row) {
                            return row.genRemove === true ? '是' : '否';
                        }
                    },
                    {
                        field: 'genEdit', title: '改', width: 100, align: 'center',
                        templet: function (row) {
                            return row.genEdit === true ? '是' : '否';
                        }
                    },
                    {
                        field: 'genSelect', title: '查', width: 100, align: 'center',
                        templet: function (row) {
                            return row.genSelect === true ? '是' : '否';
                        }
                    },
                    {
                        field: 'genPage', title: '页面', width: 100, align: 'center',
                        templet: function (row) {
                            return row.genPage === true ? '是' : '否';
                        }
                    },
                    {
                        field: 'genPageTableType', title: '表格', width: 100, align: 'center',
                        templet: function (row) {
                            return row.genPageTableType === '1' ? '分页' : '列表';
                        }
                    },
                    {
                        field: 'genPageFormType', title: '表单', width: 100, align: 'center',
                        templet: function (row) {
                            return row.genPageFormType === '1' ? '单列' : '双列';
                        }
                    },
                    {field: 'genPath', title: '生成路径', width: 100, align: 'center'},
                    {field: 'parentMenuId', title: '上级菜单', width: 100, align: 'center'},
                    {field: 'remark', title: '备注', width: 100, align: 'center'},
                    {field: 'createTime', title: '创建时间', width: 200, align: 'center', sort: true},
                    {fixed: 'right', title: '操作', minWidth: 300, align: 'center', templet: '#tableRowTool'}
                ]
            ],
            page: {
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'], // 自定义分页布局
                limit: 10,
                limits: [5, 10, 20, 50, 100],
                curr: 1,
                groups: 5, // 只显示5个连续页码
                first: false,
                last: false
            },
            loading: true,
            skin: 'grid', // 边框风格
            size: 'md', // 尺寸
            even: true, // 隔行背景
            before: function (options) { // 数据渲染之前的回调函数

            },
            done: function (res, curr, count, origin) { // 数据渲染完毕的回调函数

            },
            error: function (e, msg) { // 数据请求失败的回调函数

            },
            parseData: function (res) { // 数据格式解析的回调函数
                return {
                    "code": res.code === 200 ? 0 : res.code,
                    "msg": res.msg,
                    "count": res.data.total,
                    "data": res.data.results
                }
            },
        });

        // 监听表格工具条
        table.on('toolbar(dataTable)', function (obj) {
            if (obj.event === 'add') {
                layer.open({
                    type: 2,
                    title: '新建表',
                    shadeClose: true,
                    scrollbar: false,
                    area: ['100%', '100%'],
                    content: '/genTable/addForm'
                });
            }
            if (obj.event === 'import') {
                layer.open({
                    type: 2,
                    title: '导入表',
                    shadeClose: true,
                    scrollbar: false,
                    area: ['500px', '500px'],
                    content: '/genTable/importForm'
                });
            }
            if (obj.event === 'batchRemove') {
                var checkRows = table.checkStatus('dataTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请先选择数据', {icon: 0});
                    return;
                }
                layer.confirm('确定删除所选数据吗？', {
                    btn: ['确定', '关闭']
                }, function () {
                    var ids = checkRows.data.map(function (row) {
                        return row.id;
                    });
                    $.ajax({
                        url: '/genTable/remove',
                        type: 'DELETE',
                        contentType: 'application/json',
                        dataType: 'json',
                        data: JSON.stringify({idList: ids}),
                        success: function (response) {
                            if (response.code === 200) {
                                layer.msg('删除成功', {icon: 1});
                                tableObj.reload();
                            } else {
                                layer.msg(response.msg, {icon: 0});
                            }
                        },
                        error: function (xhr, status, error) {
                            layer.msg('请求失败', {icon: 0});
                        }
                    });
                });
            }
        });

        // 监听表格行工具
        table.on('tool(dataTable)', function (obj) {
            if (obj.event === 'edit') {
                layer.open({
                    type: 2,
                    title: '配置表',
                    shadeClose: true,
                    scrollbar: false,
                    area: ['100%', '100%'],
                    content: '/genTable/editForm/' + obj.data.id
                });
            }
            if (obj.event === 'remove') {
                layer.confirm('确定删除所选数据吗？', {
                    btn: ['确定', '关闭']
                }, function () {
                    $.ajax({
                        url: '/genTable/remove',
                        type: 'DELETE',
                        contentType: 'application/json',
                        dataType: 'json',
                        data: JSON.stringify({idList: [obj.data.id]}),
                        success: function (response) {
                            if (response.code === 200) {
                                layer.msg('删除成功', {icon: 1});
                                tableObj.reload();
                            } else {
                                layer.msg(response.msg, {icon: 0});
                            }
                        },
                        error: function (xhr, status, error) {
                            layer.msg('请求失败', {icon: 0});
                        }
                    });
                });
            }
            if (obj.event === 'genLocal') {
                $.ajax({
                    url: '/genTable/genLocal/' + obj.data.id,
                    type: 'POST',
                    success: function (response) {
                        if (response.code === 200) {
                            layer.msg('代码生成成功', {icon: 1});
                        } else {
                            layer.msg(response.msg, {icon: 0});
                        }
                    },
                    error: function (xhr, status, error) {
                        layer.msg('请求失败', {icon: 0});
                    }
                });
            }
            if (obj.event === 'genDownload') {
                const xhr = new XMLHttpRequest();
                xhr.open('GET', '/genTable/genDownload/' + obj.data.id, true);
                xhr.responseType = 'blob';

                xhr.onload = function () {
                    if (xhr.status === 200) {
                        // 获取 Blob 对象
                        const blob = xhr.response;

                        // 创建一个指向 Blob 对象的 URL
                        const urlObject = URL.createObjectURL(blob);

                        // 创建一个 <a> 元素并模拟点击以触发下载
                        const a = document.createElement('a');
                        a.href = urlObject;
                        a.download = 'code.zip';
                        document.body.appendChild(a);
                        a.click();

                        // 清理
                        setTimeout(() => {
                            document.body.removeChild(a);
                            window.URL.revokeObjectURL(urlObject);
                        }, 0);
                    } else {
                        layer.msg('请求失败', {icon: 0});
                    }
                };

                xhr.onerror = function () {
                    layer.msg('请求失败', {icon: 0});
                };

                xhr.send();
            }
        });

        // 监听查询
        form.on('submit(tableSearch)', function (data) {
            tableObj.reload({page: {curr: 1}, where: data.field});
            return false;
        });
    });
</script>
</html>